package com.lfz.rabbit.direct;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitClient {

    @Autowired
    AmqpTemplate amqpTemplate;

    /**
     * 指定交换机、RoutingKey发送消息
     * @param message 发送的消息字符串
     * @param exchangetName 交换机名称
     * @param routingKey RoutingKey
     */
    public void send(String exchangetName, String routingKey, String message) {
        amqpTemplate.convertAndSend(exchangetName, routingKey, message);

    }

    /**
     * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key.
     *
     * @param message a message to send
     * @throws AmqpException if there is a problem
     */
    public void send(Object message) {
        amqpTemplate.convertAndSend(message);
    }

    /**
     * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.
     *
     * @param routingKey the routing key
     * @param message a message to send
     * @throws AmqpException if there is a problem
     */
    public void send(String routingKey, Object message) throws AmqpException {
        amqpTemplate.convertAndSend(routingKey, message);
    }



}
